package com.lianxian.xindong.commonres.utils;

import android.text.TextUtils;

import com.hxb.library.utils.HxbUtils;
import com.hxb.library.utils.LogUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import com.lianxian.xindong.commonres.entity.RequestBodyBean;

/**
 * @Author :hexingbo
 * @Date :2019/4/18
 * @FileName： RequestBodyUtil
 * @Describe :
 */
public class RequestBodyUtil {

    //////////////////////////////////////////////////新构建参数与方法封装/////////////////////////////////////////////////////////////////////////////

    private static MultipartBody.Builder builder;

    private static MultipartBody.Builder getBuilder() {
        if (builder == null) {
            builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        }
        return builder;
    }


    /**
     * 上传文件（单个）
     *
     * @param file
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, File file) {
        builder = null;
        return builderFileValue(getBuilder(), fileKey, file).build();
    }

    /**
     * 上传文件（多个/单个）
     *
     * @param fileKey  默认files
     * @param fileList
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, List<File> fileList) {
        builder = null;
        return builderFilesValue(getBuilder(), fileKey, fileList).build();
    }

    /**
     * key:value
     *
     * @param mapValue
     * @return
     */
    public static MultipartBody getMultipartBody(Map<String, Object> mapValue) {
        builder = null;
        return builderBodyMapValue(getBuilder(), mapValue).build();
    }

    /**
     * key:value
     *
     * @param bodyBean
     * @return
     */
    public static MultipartBody getMultipartBody(RequestBodyBean bodyBean) {
        builder = null;
        return builderBodyValue(getBuilder(), bodyBean).build();
    }

    /**
     * 单文件+key:value
     *
     * @param fileKey  默认files
     * @param file
     * @param mapValue
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, File file, Map<String, Object> mapValue) {
        builder = null;
        return builderBodyMapValue(builderFileValue(getBuilder(), fileKey, file), mapValue).build();
    }

    /**
     * 单文件+key:value
     *
     * @param fileKey  默认files
     * @param file
     * @param bodyBean
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, File file, RequestBodyBean bodyBean) {
        builder = null;
        return builderBodyValue(builderFileValue(getBuilder(), fileKey, file), bodyBean).build();
    }

    /**
     * 单文件+key:value
     *
     * @param fileKey  默认files
     * @param fileList
     * @param mapValue
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, List<File> fileList, Map<String, Object> mapValue) {
        builder = null;
        return builderBodyMapValue(builderFilesValue(getBuilder(), fileKey, fileList), mapValue).build();
    }

    /**
     * 单文件+key:value
     *
     * @param fileList
     * @param bodyBean
     * @return
     */
    public static MultipartBody getMultipartBody(String fileKey, List<File> fileList, RequestBodyBean bodyBean) {
        builder = null;
        return builderBodyValue(builderFilesValue(getBuilder(), fileKey, fileList), bodyBean).build();
    }


    /**
     * 上传多文件也支持单文件
     *
     * @param file
     * @return
     */
    private static MultipartBody.Builder builderFileValue(MultipartBody.Builder builder, String fileKey, File file) {
        if (builder == null) {
            builder = getBuilder();
        }
        if (file != null)
            if (FileSizeUtil.getFileOrFilesSize(file.getPath(), FileSizeUtil.SIZETYPE_KB) >= 1024) {
                if (ImageUtil.qualityCompress(ImageUtil.fileToBitmap(file.getPath()), 500, file)) {
                    addFormDataPart(builder, fileKey, file);
                }
            } else {
                addFormDataPart(builder, fileKey, file);
            }
        return builder;
    }

    /**
     * 上传多文件也支持单文件
     *
     * @param list
     * @return
     */
    private static MultipartBody.Builder builderFilesValue(MultipartBody.Builder builder, String fileKey, List<File> list) {
        if (builder == null) {
            builder = getBuilder();
        }
        if (!HxbUtils.isEmpty(list)) {
            //注意，file是后台约定的参数，如果是多图，file[]，如果是单张图片，file就行
            for (int i = 0; i < list.size(); i++) {
                File file = list.get(i);
                if (FileSizeUtil.getFileOrFilesSize(file.getPath(), FileSizeUtil.SIZETYPE_KB) >= 1024) {
                    if (ImageUtil.qualityCompress(ImageUtil.fileToBitmap(file.getPath()), 500, file)) {
                        addFormDataPart(builder, fileKey, file);
                    }
                } else {
                    addFormDataPart(builder, fileKey, file);
                }
            }
        }
        return builder;
    }


    protected static void addFormDataPart(MultipartBody.Builder builder, String fileKey, File file) {
        if (builder == null) {
            builder = getBuilder();
        }
        LogUtils.debugInfo("files--->", file.getName());
        //json : application/json
        //xml : application/xml
        //png : image/png
        //jpg : image/jpeg
        //gif : imge/gif
        //application/octet-stream  即指定为二进制流
        // 文件格式：注：file为上传文件，"application/octet-stream" 即指定为二进制流，不知道具体下载文件类型
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream; charset=utf-8"),file);
//        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        builder.addFormDataPart(TextUtils.isEmpty(fileKey) ? "files" : fileKey, file.getName(), requestBody);
    }

    /**
     * 普通body提交参数
     *
     * @param bodyBean
     * @return
     */
    private static MultipartBody.Builder builderBodyValue(MultipartBody.Builder builder, RequestBodyBean bodyBean) {
        if (builder == null) {
            builder = getBuilder();
        }
        return builderBodyMapValue(builderBodyListValue(builder, bodyBean.getMapList()), bodyBean.getMapValue());
    }

    /**
     * 普通body提交参数
     *
     * @param mapValue 一个key对应一个value
     * @return
     */
    private static MultipartBody.Builder builderBodyMapValue(MultipartBody.Builder builder, Map<String, Object> mapValue) {
        if (builder == null) {
            builder = getBuilder();
        }
        if (!HxbUtils.isEmpty(mapValue)) {
            for (String key : mapValue.keySet()) {
                builder.addFormDataPart(key, mapValue.get(key) + "");
            }
        }
        return builder;
    }

    /**
     * 普通body提交参数
     *
     * @param mapListValue 一个Key对应多个value
     * @return
     */
    private static MultipartBody.Builder builderBodyListValue(MultipartBody.Builder builder, Map<String, List<String>> mapListValue) {
        if (builder == null) {
            builder = getBuilder();
        }
        if (!HxbUtils.isEmpty(mapListValue)) {
            for (String key : mapListValue.keySet()) {
                List<String> list = mapListValue.get(key);
                if (!HxbUtils.isEmpty(mapListValue)) {
                    for (String value : list) {
                        builder.addFormDataPart(key, value);
                    }
                }
            }
        }
        return builder;
    }
}
